<?php
namespace Admin\Model;
use Think\Model;
class ActivityModel extends Model 
{
	protected $insertFields = array('user_id','gene_time','place','number','sex','budget','status','text','theme','end_time','age','user_type','shop_id');
	protected $updateFields = array('act_id','user_id','gene_time','place','number','sex','budget','status','text','theme','end_time','age','user_type','shop_id');
	protected $_validate = array(
//		array('user_id', 'require', '用户的id不能为空！', 1, 'regex', 3),
//		array('user_id', 'number', '用户的id必须是一个整数！', 1, 'regex', 3),
//		array('gene_time', 'require', '开始时间不能为空！', 1, 'regex', 3),
//		array('text', 'require', '活动介绍不能为空！', 1, 'regex', 3),
//		array('theme', 'require', '主题不能为空！', 1, 'regex', 3),
//		array('theme', '1,50', '主题的值最长不能超过 50 个字符！', 1, 'length', 3),
//		array('user_type', 'require', '1是商家2是用户不能为空！', 1, 'regex', 3),
//		array('user_type', 'number', '1是商家2是用户必须是一个整数！', 1, 'regex', 3),
//		array('shop_id', 'number', '店铺的id必须是一个整数！', 2, 'regex', 3),
	);
	public function search($pageSize = 2)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.act_id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['picture']) && $_FILES['picture']['error'] == 0)
		{
			$ret = uploadOne('picture', 'Admin', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['picture'] = $ret['images'][0];
				$data['big_picture'] = $ret['images'][1];
				$data['mid_picture'] = $ret['images'][2];
				$data['sm_picture'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
		if(isset($_FILES['picture']) && $_FILES['picture']['error'] == 0)
		{
			$ret = uploadOne('picture', 'Admin', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['picture'] = $ret['images'][0];
				$data['big_picture'] = $ret['images'][1];
				$data['mid_picture'] = $ret['images'][2];
				$data['sm_picture'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_picture'),
				I('post.old_big_picture'),
				I('post.old_mid_picture'),
				I('post.old_sm_picture'),
	
			));
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['act_id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('picture,big_picture,mid_picture,sm_picture')->find($option['where']['act_id']);
		deleteImage($images);
	}
	/************************************ 其他方法 ********************************************/
}